import marsConfig from "./mars-config.js";

//引入cesium基础库
import "mars3d-cesium/Build/Cesium/Widgets/widgets.css";
import * as Cesium from "mars3d-cesium";

//导入mars3d主库
import "mars3d/dist/mars3d.css";
import * as mars3d from "mars3d";

//导入mars3d插件（按需使用，需要先npm install）
import "mars3d-space";

export default {
	init(dom = "mars3dContainer") {
		let map = new mars3d.Map(dom, marsConfig);
		let toolMars = {
			popup(msg = "地图弹窗消息...", position = [114.547015, 34.506371, 1521]) {
			  // 传入坐标和内容，可以直接任意弹出
			  map.openPopup(position, msg);
			  toolMars.setCameraView({
			  	alt: 17000
			  });
			},
			addImg(option) {
				toolMars.layer(new mars3d.layer.ImageLayer(Object.assign({
					url: "//data.mars3d.cn/file/img/zkd-xq.png",
					rectangle: {
						xmin: 117.245648,
						xmax: 117.254431,
						ymin: 31.836891,
						ymax: 31.843413
					},
					zIndex: 20
				}, option)));
				toolMars.setCameraView({
					lat: 31.836891,
					lng: 117.245648,
					alt: 17000
				});
			},
			getCameraView: () => map.getCameraView(),
			layer: (layer) => {
				if (!layer) {
					return map.getLayers({
						basemaps: true, // 是否取config.json中的basempas
						layers: true // 是否取config.json中的layers
					});
				}
				map.addLayer(layer);
			},
			/**
			 * 区域蒙层
			 * @param {Object} option
			 */
			areaMask(option) {
				map.addLayer(new mars3d.layer.GeoJsonLayer(Object.assign({
					url: "//data.mars3d.cn/file/geojson/areas/340000.json",
					mask: true, // 标识为遮罩层【重点参数】
					symbol: {
						styleOptions: {
							fill: true,
							color: "rgb(2,26,79)",
							opacity: 0.8,
							outline: true,
							outlineColor: "#39E09B",
							outlineWidth: 8,
							outlineOpacity: 0.8,
							arcType: Cesium.ArcType.GEODESIC,
							clampToGround: true
						}
					}
				}, option)));
			},
			/**
			 * 区域墙
			 * @param {Object} option
			 */
			areaWall(option) {
				toolMars.layer(new mars3d.layer.GeoJsonLayer(Object.assign({
					url: "//data.mars3d.cn/file/geojson/areas/340000.json",
					mask: true, // 标识为遮罩层【重点参数】
					symbol: {
						type: "wallP",
						styleOptions: {
							setHeight: -150000,
							diffHeight: 150000, // 墙高
							materialType: mars3d.MaterialType.Image2,
							materialOptions: {
								image: "http://mars3d.cn/img/textures/fence-top.png",
								color: "rgba(0,255,255,0.6)"
							}
						}
					}
				}, option)));
			},
			setCameraView(options = {}) {
				map.setCameraView({
					lat: 34.506371,
					lng: 114.547015,
					alt: 2192496,
					heading: 0,
					pitch: -90,
					...options
				})
			}
		}
		return toolMars;
	}
}
